//
//  MyTable.m
//  MyBook
//
//  Created by zhiyou on 16-3-19.
//  Copyright (c) 2016年 zhiyou. All rights reserved.
//

#import "MyTable.h"
#import "Book.h"
#import "Users.h"
@implementation MyTable
//创建bookTable
+ (void)createBookTable1
{
    // 1.获取数据库对象
    FMDatabase *db  = [Tools getDatabase];
    // 2.判断数据是否打开
    // [db open]返回值为YES,数据库打开成功
    if ([db open] == YES) {
        // 判断表是否存在
        // 表存在 返回YES,不存返回NO
        // 表不存在,再去创建
        if([db tableExists:@"Book"] == NO)
        {
            // 执行更新sql语句
            // 判断执行结果,返回值为YES,执行成功,为NO失败
            if ([db executeUpdate:@"create table Book (Book_id integer primary key,Book_name text,Book_author text,Book_bookImagename text,Book_detail text,Book_kind text,Book_link text)"] == YES)
            {
                NSLog(@"创建成功");
            }else
            {
                NSLog(@"创建失败");
            }
        }else
        {
            NSLog(@"表已经存在,不可重复创建!");
        }
        // 关闭数据库
        [db close];
        
    }
}
//添加书籍信息
+(void)Addbookname:(NSString*)name author:(NSString*)author kind:(NSString*)kind imagename:(NSString*)imagename Detail:(NSString *)detail booklink:(NSString *)link{
    NSString*sql=[NSString stringWithFormat:@"insert into Book(Book_name, Book_author ,Book_bookImagename ,Book_detail ,Book_kind,Book_link)values('%@','%@','%@','%@','%@','%@')",name,author,imagename ,detail,kind,link];
    FMDatabase*db=[Tools getDatabase];
    if([db open]){
        if([db executeUpdate:sql]){
            NSLog(@"添加成功");
        }else{
            NSLog(@"添加失败");
        }
    }
    [db close];
}
+(NSMutableArray*)selectAllBook{
   
     NSMutableArray*array=[[NSMutableArray alloc]init];
    FMDatabase*db=[Tools getDatabase];
    FMResultSet*set=[db executeQuery:@"select*from Book"];
    
    if([db open]){
            while ([set next]) {
                Book*book=[[Book alloc]init];
                book.bookname=[set stringForColumn:@"Book_name"];
                book.author=[set stringForColumn:@"Book_author"];
                book.ImageName=[set stringForColumn:@"Book_bookImagename"];
                book.detail=[set stringForColumn:@"Book_detail"];
                book.kind=[[set stringForColumn:@"Book_kind"]intValue];
                book.link=[set stringForColumn:@"Book_link"];
                [array addObject:book];
                                          }
        
        [db close];
    }
    return array;
}
@end
